package com.heima.wemedia.controller.v1;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.heima.apis.wmuser.WmUserControllerApi;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.media.pojos.WmUser;
import com.heima.wemedia.service.WmUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/v1/user")
public class WmUserController implements WmUserControllerApi {

    @Autowired
    private WmUserService userService;


    /**
     * 保存自媒体用户
     * @param wmUser
     * @return
     */
    @PostMapping("/save")
    @Override
    public ResponseResult saveOrUpdate(@RequestBody WmUser wmUser) {
        System.out.println("wmUser"+wmUser);
        userService.saveOrUpdate(wmUser);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }


    /**
     * 按照名称查询用户
     * @param name
     * @return
     */
    @GetMapping("/findByName/{name}")
    @Override
    public WmUser findByName(@PathVariable("name") String name) {


        LambdaQueryWrapper<WmUser> wrapper = Wrappers.<WmUser>lambdaQuery().eq(WmUser::getName, name);
        List<WmUser> list = userService.list(wrapper);
        if (list!=null&&!list.isEmpty()){
            return list.get(0);
        }
        return null;
    }
}
